<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
</head>

<body>
    <button onclick="showRoadState()">显示实时交通</button>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 12
            })
        })
        const style = new ol.style.Style({
            stroke: new ol.style.Stroke({
                width: 3,
                color: "green"
            })
        })
        var url = 'http://39.103.151.139:8000/gis/road_state'
        const geo_format = new ol.format.GeoJSON()
        // 根据geojson数据的url和format_geo，自动生成带有features的数据源
        var source = new ol.source.Vector({
            format: geo_format,
            url
        })
        var layer = new ol.layer.Vector({
            source,
            style
        })
        map.addLayer(layer)

        /* 1、点击按钮--获取所有的要素 */
        /* 2、遍历要素 获取flow */
        /* 3、根据flow值给要素设置不同的颜色 */
        /* flow>1000 red
        <1000 green
         */
        function showRoadState() {
            /* 1、features */
            let features = source.getFeatures();
            console.log(features, 'features')// [e,e,e……]
            /* 2、遍历 */
            features.forEach(feature => {
                let color = null;
                /* 3、根据flow值给要素设置不同的颜色 */
                var flow = feature.get("flow");
                //×// var flow = feature.getFlow();//× 后加的属性没有这种方法
                console.log(flow, 'flow')
                const properties = feature.getProperties()
                const values_ = feature.values_
                console.log(properties, 'feature.getProperties')
                console.log(values_, 'feature.values_')
                console.log(properties === values_, 'properties===values_')// false
                console.log(isShallowCopy(properties, values_),'isShallowCopy')// true
                if (flow > 1000) {
                    color = "red"
                } else {
                    color = "green"
                }
                let stateStyle = new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        width: 3,
                        color
                    })
                })
                feature.setStyle(stateStyle)
            })
        }

        // 监测a是不是b的浅拷贝
        function isShallowCopy(a, b) {
                // 首先确保 a 和 b 是对象类型，并且不是同一个引用
                if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
                    return false;
                }

                // 获取对象 a 和 b 的所有属性
                const keysA = Object.keys(a);
                const keysB = Object.keys(b);

                // 检查属性数量是否相同
                if (keysA.length !== keysB.length) {
                    return false;
                }

                // 检查每个属性的值是否相同
                for (let key of keysA) {
                    // 如果对象 b 没有相同的属性，或者属性值不同，则返回 false
                    if (!keysB.includes(key) || a[key] !== b[key]) {
                        return false;
                    }
                }

                // 如果所有属性值都相同，则返回 true
                return true;
            }
    </script>
</body>

</html>